<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #cate_id="{ record }">{{ record.cate_name }}</template>
      <template #file="{ record }">
          <!-- 对标题加上 tag -->
          <div v-if="record.file != null">
            <a-link v-for="(item,index) in record.file" :key="index" :herf="item">附件({{ index + 1 }})</a-link>
          </div>
          <div v-else>
            无
          </div>
      </template>
      <template #contract_id="{ record }">{{ record.contract.name }}</template>
      <template #copy_for="{ record }">
          <div style="display:flex" v-if="record.copy_for != null">
            <div v-for="(item,index) in record.copy_list" :key="index" >{{ item.username}};</div>
          </div>
          <div v-else>
            无
          </div>
      </template>
      <template #operationBeforeExtend="{ record }" >
        <a-link @click="setShenpi(record)" v-if="(record.oa_status != 2 && record.now_user_id == user.id)"> 审批</a-link>
        <a-link @click="setbianji(record)" v-if="(record.oa_status == 2 || record.oa_status == -1)"> 重新提审</a-link>
        <a-link @click="setcexiao(record)" v-if="(record.oa_status == 1 && record.created_by == user.id)">撤销</a-link>
      </template>
    </ma-crud>

    <a-modal :mask-closable="false"  v-model:visible="spShowModle"  width="auto" title="审批"  :footer="false" :ok-loading="okloading" >
      <div style="display:flex;margin-bottom: 10px;">
        <div style="width:80px;padding:5px;">合同分类:</div>
        <div style="width:600px;padding:5px 16px;border-radius: 6px;background:#e5e7eb;color:rgba(29, 33, 41)">{{ itemData.cate_name || '无' }}</div>
      </div>
      <div style="display:flex;margin-bottom: 10px;">
        <div style="width:80px;padding:5px;">合同:</div>
        <div style="width:600px;padding:5px 16px;border-radius: 6px;background:#e5e7eb;color:rgba(29, 33, 41)">{{ itemData.contract?.name}}</div>
      </div>
      <div style="display:flex;margin-bottom: 10px;">
        <div style="width:80px;padding:5px;">审核标题:</div>
        <div style="width:600px;padding:5px 16px;border-radius: 6px;background:#e5e7eb;color:rgba(29, 33, 41)">{{ itemData.name || '无' }}</div>
      </div>
      <div style="display:flex;margin-bottom: 10px;">
        <div style="width:80px;padding:5px;">审核描述:</div>
        <div style="width:600px;padding:5px 16px;border-radius: 6px;background:#e5e7eb;color:rgba(29, 33, 41)" v-if="itemData.desc != null" v-html="itemData.desc"></div>
        <div style="width:600px;padding:5px 16px;border-radius: 6px;background:#e5e7eb;color:rgba(29, 33, 41)" v-else >无</div>
      </div>
      <div style="display:flex;margin-bottom: 10px;">
        <div style="width:80px;padding:5px;">抄送人员:</div>
        <div style="width:600px;padding:5px 16px;border-radius: 6px;background:#e5e7eb;color:rgba(29, 33, 41);display:flex" v-if="itemData.copy_for != null">
          <div v-for="(item,index) in itemData.copy_list" :key="index" >{{ item.username}};</div>
        </div>
        <div style="width:600px;padding:5px 16px;border-radius: 6px;background:#e5e7eb;color:rgba(29, 33, 41)" v-else >无</div>
      </div>
      <div style="display:flex;margin-bottom: 10px;">
        <div style="width:80px;padding:5px;">附件:</div>
        <div style="width:600px;padding:5px 0px;border-radius: 6px;" v-if="itemData.file != null">
          <div v-for="(item,index) in itemData.file" :key="index">
            <a-link :herf="item">{{ item }}</a-link>
          </div>
        </div>
        <div style="width:600px;padding:5px 16px;border-radius: 6px;background:#e5e7eb;color:rgba(29, 33, 41)" v-else >无</div>
      </div>
      <div style="margin-bottom: 50px;">
        <a-timeline >
          <a-timeline-item  :dotColor="(item.status == 0 && item.is_now == 1)?'':(item.status == 0 && item.is_now == 0)?'#e5e7eb':item.status == 1?'#52C419':'#F5222D'" v-for="(item,index) in itemDatas.detail" :key="index">
            <div style="font-size:16px;margin-bottom: 10px;">审核人：{{item.user.username}}</div>
            <div style="display:flex;margin-bottom: 10px;" v-if="item.is_now == 0">
              <div style="font-size:14px">备注：</div>
              <div >{{ item.remark || '无' }}</div>
              
            </div>
            <div style="display:flex;margin-bottom: 10px;align-items: center;" v-else>
              <div style="font-size:14px">备注：</div>
              <div style="flex:1;width:0;">
                <input v-model="itemDatas.detail[index].remark" type="text" class="input"/>
              </div>
            </div>
            <div style="color:rgba(134, 144, 156);margin-bottom: 10px;">{{ item.status == 0?'': item.updated_at }}</div>
          </a-timeline-item>
        </a-timeline>
      </div>
      <div style="display:flex;margin-bottom: 10px;align-items: center;justify-content: flex-end;">
        <div style="margin-right: 20px;">
          <a-button type="primary" status="danger" @click="handleBohui">驳回</a-button>
        </div>
        <a-button type="primary" @click="handleTongguo">审核通过</a-button>
      </div>
    </a-modal>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import oaManage from '@/api/oa/oaManage'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'
import { useUserStore } from '@/store'
const userStore = useUserStore()
// 当前登录的用户所有信息，包括 id 用户名 昵称 等信息
const user = userStore.user
let okloading = false
let itemData = {};
let itemDatas = {};
const crudRef = ref()

const spShowModle = ref(false);

const numberOperation = (newValue, id, numberName) => {
  oaManage.numberOperation({ id, numberName, numberValue: newValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}

const switchStatus = (statusValue, id, statusName) => {
  oaManage.changeStatus({ id, statusName, statusValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}

// 唤起审批弹窗
const setShenpi = (item) => {
  oaManage.read({id:item.id}).then(res=>{
    itemDatas = res.data
    itemData = item
    spShowModle.value = true
  })
  
}

// 唤起编辑弹窗
const setbianji = (record) =>{
  crudRef.value.editAction(record)
}

// 撤销
const setcexiao = (record) =>{
  oaManage.cancelOa({id:record.id}).then(res=>{
    res.success && Message.success(res.message)
    crudRef.value.refresh()
  })
}

// 审核通过
const handleTongguo = () => {
  let oaData = {};
  itemDatas.detail.map((v,i)=>{
    if(v.is_now == 1){
      oaData.id = v.id
      oaData.oa_id = v.oa_id
      oaData.remark = v.remark
      oaData.status = 1
    }
  })
  oaManage.checkOa(oaData).then(res=>{
    spShowModle.value = false 
    crudRef.value.refresh()
  })
  
}

// 审核驳回
const handleBohui = () => {
  let oaData = {};
  itemDatas.detail.map((v,i)=>{
    if(v.is_now == 1){
      oaData.id = v.id
      oaData.oa_id = v.oa_id
      oaData.remark = v.remark
      oaData.status = 2
    }
  })
  
  oaManage.checkOa(oaData).then(res=>{
    spShowModle.value = false
    crudRef.value.refresh()
  })
}


const options = reactive({
  id: 'flyos_oa_manage',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 1000
  },
  api: oaManage.getList,
  recycleApi: oaManage.getRecycleList,
  add: {
    show: true,
    text:'新增审核',
    api: oaManage.save,
    auth: ['oa:manage:save']
  },
  edit: {
    show: false,
    api: oaManage.update,
    auth: ['oa:manage:update']
  },
  delete: {
    show: false,
    api: oaManage.deletes,
    auth: ['oa:manage:delete'],
    realApi: oaManage.realDeletes,
    realAuth: ['oa:manage:realDeletes']
  },
  recovery: {
    show: true,
    api: oaManage.recoverys,
    auth: ['oa:manage:recovery']
  },
  import: {
    show: true,
    url: 'oa/manage/import',
    templateUrl: 'oa/manage/downloadTemplate',
    auth: ['oa:manage:import']
  },
  export: {
    show: true,
    url: 'oa/manage/export',
    auth: ['oa:manage:export']
  }
})

const columns = reactive([
  {
    title: "ID",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入ID"
    }
  },
  // {
  //   title: "分类ID",
  //   dataIndex: "cate_id",
  //   formType: "input",
  //   search: true,
  //   commonRules: {
  //     required: true,
  //     message: "请输入分类ID"
  //   }
  // },
  {
    title: "审核流程",
    dataIndex: "cate_id",
    formType: "select",
    search: true,
    dict: {
        // 远程通用接口请求，新版代码生成都有一个 remote 接口
        remote: 'oa/cate/remote',
        // 指定组件接收的props
        props: { label: 'name', value: 'id' },
        // 远程请求配置项
        remoteOption: {
            // 设置查询的字段
            select: [ 'id', 'name' ],
            // 设置数据过滤
            filter: {
            },
            // 关联模型
            relations: [
                // 其他关联....其他可查看 MapperTiart.php 的源代码自行参考
            ]
        }
    }
  },
  {
    title: "合同",
    dataIndex: "contract_id",
    formType: "select",
    search: true,
    dict: {
        // 远程通用接口请求，新版代码生成都有一个 remote 接口
        remote: 'system/userContract/remote',
        // 指定组件接收的props
        props: { label: 'name', value: 'id' },
        // 远程请求配置项
        remoteOption: {
            // 设置查询的字段
            select: [ 'id', 'name' ],
            // 设置数据过滤
            filter: {
            },
            // 关联模型
            relations: [
                // 其他关联....其他可查看 MapperTiart.php 的源代码自行参考
            ]
        }
    }
  },
  {
    title: "审核标题",
    dataIndex: "name",
    formType: "input",
    search: true,
    commonRules: {
      required: true,
      message: "请输入审核标题"
    }
  },
  {
    title: "审核描述",
    dataIndex: "desc",
    formType: "editor",
    search: true,
    hide:true
  },
  {
    title: "抄送人员",
    dataIndex: "copy_for",
    formType: "select",
    multiple: true,
    search: true,
    dict: {
        // 远程通用接口请求，新版代码生成都有一个 remote 接口
        remote: 'system/user/remote',
        // 指定组件接收的props
        props: { label: 'username', value: 'id' },
        // 远程请求配置项
        remoteOption: {
            // 设置查询的字段
            select: [ 'id', 'username' ],
            // 设置数据过滤
            filter: {
            },
            // 关联模型
            relations: [
                // 其他关联....其他可查看 MapperTiart.php 的源代码自行参考
            ]
        }
    }
  },
  {
    title: "附件",
    dataIndex: "file",
    formType: "upload",
    multiple: true,
    type:'file',
    limit: 9,
    returnType:"url",
  },
  {
    title: "审核状态",
    dataIndex: "oa_status",
    formType: "radio",
    search: true,
    addDisplay: false,
    editDisplay: false,
    dict: {
      name: "oa_status",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    }
  },
  {
    title: "当前审核人",
    dataIndex: "now_username",
    formType: "input",
    search: true,
    addDisplay: false,
    editDisplay: false
  },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "修改时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "更新者",
    dataIndex: "updated_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  }
])
</script>
<style>

  .input{
    width:100%;
    padding:4px 12px;
    border:1px solid #e5e7eb;
    /* background: rgba(242,243,245); */
    border-radius: 4px;
    outline: none;/*清除input点击之后的黑色边框*/
  }

</style>
<script> export default { name: 'oa:manage' } </script>